<?php
namespace jy_cun9bao;
defined('BY_JYA') or exit('error');
/*
	分红佣金
	
*/
class commission extends \plugin{
	/*
		生成佣金
	*/
	function create($order){
		$is_open=intval($this->setting['is_open']);
		if($is_open==0){
			return;
		}
		$order['fee']=$order['fee']>0?$order['fee']:$order['price'];
		$this->order=$order;
		$member=pdo_get('core_members',array('id'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
		$member['mid']=$member['id'];
		$fan_member=pdo_get('jy_cun9bao_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
		$fan_member['agent_id']=$member['fromuser'];
		$member=array_merge($member,$fan_member);
		if($member['level_id']){
			$this->nowlevel=$this->getcolumn('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']),'level');
		}else{
			$this->nowlevel=0;
		}
		//自购升级
		if($member['is_agent']){
			$this->model('member')->update_agent_level($order['mid'],$order['id']);
		}
		if($this->order['fee']<=0){
			return;
		}
		$this->commission_order=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$this->order['mid'],
				'agent_id'=>$member['agent_id']?:$this->order['mid'],
				'ordersn'=>$this->order['id'],
				'price'=>$this->order['fee'],
				'plugin_name'=>$this->order['plugin_name']?:'',
				'commission_price'=>$this->order['fee'],
				'commission_money'=>0,
				'fee2'=>0,
				'ispay'=>1,
				'createtime'=>TIMESTAMP,
		);
		$agent_id=$member['agent_id'];
		$member=pdo_get('core_members',array('id'=>$agent_id,'uniacid'=>$_SESSION['uniacid']));
		$member['mid']=$member['id'];
		$fan_member=pdo_get('jy_cun9bao_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$agent_id));
		$fan_member['agent_id']=$member['fromuser'];
		$member=array_merge($member,$fan_member);
		$level=$this->get('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']));
		$this->create_commission($member,1,$level);
		$agent_id=$member['agent_id'];
		if($agent_id){
			$member=pdo_get('core_members',array('id'=>$agent_id,'uniacid'=>$_SESSION['uniacid']));
			$member['mid']=$member['id'];
			$fan_member=pdo_get('jy_cun9bao_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$agent_id));
			$fan_member['agent_id']=$member['fromuser'];
			$member=array_merge($member,$fan_member);
			$level=$this->get('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']));
			$this->create_commission($member,2,$level);
		}
		//生成分红订单
		$indata=$this->commission_order;
		unset($indata['fee2']);
		$this->insert('order',$indata);
		return true;
	}
	function create_commission($member,$commission_level=1,$level=array()){
		if($member && $member['is_agent'] && $member['stop']==0){
			//相应会员存在且是分销商且状态正常			
			if(!$member['level_id']){
				//获取默认等级佣金比例
				$level=$this->plugin_setting('default_level');
				$level['level']=0;
				$level['id']=0;
				$level_id=0;
			}else{
				$level_id=$level['id'];
			}
			//file_put_contents(ROOT_D.'/1.txt','~isping~'.$isping);
			$fee2=0;
			$order_price=$this->commission_order['commission_price'];
			$per2=$commission_level==2?$level['red_rate2']:$level['red_rate'];
			$remark=array(
				'level_id'=>$level_id,
				'commission_level'=>$commission_level,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
			);
			if(substr($per2,-1)=='%'){
				$fee2=intval($order_price*substr($per2,0,-1))/100;
				$fee2 -=$this->commission_order['fee2'];
			}else{
				$fee2=$per2;
				$fee2 -=$this->commission_order['fee2'];
			}
			if($fee2>0){
				$this->commission_order['commission_money']+=$fee2;
				$remark['commission_money']=$fee2;
				if($member['lastredfee']>=$fee2){
					$type=1;
					$redfee=$fee2;
				}elseif($member['lastredfee']>0 && $member['lastredfee']<$fee2){
					$type=1;
					$redfee=$member['lastredfee'];
					$fee2=$fee2-$member['lastredfee'];
				}
				//所有都从释放额里面释放
				if($fee2>0 && false){
					$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$member['mid'],
							'level'=>$member['level_id'],
							'ordersn'=>$this->order['id'],
							'money'=>$fee2,
							'type'=>$commission_level==2?6:2,
							'remark'=>ijson_encode($remark),
							'createtime'=>TIMESTAMP,
							'status'=>2,
					);
					$this->insert('commission',$new_commission);
					cfc('core')->member_credit($member['mid'],'credit2',$fee2,1,'jy_cun9bao','分销奖励');
				}
				if($redfee>0){
					$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$member['mid'],
							'level'=>$member['level_id'],
							'ordersn'=>$this->order['id'],
							'money'=>$redfee,
							'type'=>$commission_level==2?5:1,
							'remark'=>ijson_encode($remark),
							'createtime'=>TIMESTAMP,
							'status'=>2,
					);
					$this->insert('commission',$new_commission);
					pdo_update('jy_cun9bao_member',array('lastredfee -='=>$redfee),array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']));
					cfc('core')->member_credit($member['mid'],'credit2',$redfee,1,'jy_cun9bao','分销释放余额');
					$this->model('model')->up_feelog($redfee,6);
				}
			}
			
		}
		return 0;
	}
}